//
//  NewFeatureCollectionViewController.swift
//  Sinaweibo
//
//  Created by 谌桃 on 15/11/30.
//  Copyright © 2015年 ryan.chen. All rights reserved.
//

import UIKit
import SnapKit

private let maxImageCount = 4
private let reuseIdentifier = "Cell"

class NewFeatureCollectionViewController: UICollectionViewController {
    
    init(){
        
        //设置流水布局样式, 不设就会崩溃
        let layout = UICollectionViewFlowLayout()
        
        //设置cell的大小
        layout.itemSize = UIScreen.mainScreen().bounds.size
        
        //设置滚动方向
        layout.scrollDirection = .Horizontal
        
        //设置item的间距
        layout.minimumLineSpacing = 0
        
        
        super.init(collectionViewLayout: layout)
        
        //必须在super.init之后才能 使用collectionView
        
        //取消弹簧
        collectionView?.bounces = false
        //设置分页
        collectionView?.pagingEnabled = true
        //取消水平指示器
        //        collectionView.
    }
    
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        
        super.init(coder: aDecoder)
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //注册cell的类型
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
    
    // MARK: UICollectionViewDataSource
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return maxImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //使用自定义cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        
        //赋值
        cell.index = indexPath.item
        
        return cell
    }
    
    //item已经出现
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //打印的是当前选中的上一个
//        print(indexPath.item)
        
        //最后一个可见cell
       let cell = collectionView.visibleCells().last as! NewFeatureCell
        
        //取出cell对应的index
       let index = collectionView.indexPathForCell(cell)
        
        let indexItem = index?.item ?? 0
        
        if indexItem == maxImageCount - 1 {
            
            cell.startAnimation()
        }
    }
}


//MARK:自定义cell

class NewFeatureCell: UICollectionViewCell{
    
    var index:Int = 0{
    
        didSet{
        
            //设置iconview 的图片, 图片的名称通过 对应Item 获取
            iconView.image = UIImage(named: "new_feature_\(index+1)")
            
            startBtn.hidden = true
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        layoutUI()
     
    }
    
    required init?(coder aDecoder: NSCoder) {
        //        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    //给开始体验按钮添加动画效果
    private func startAnimation(){
        
        startBtn.hidden = false
        
        //设置初始形变
       startBtn.transform = CGAffineTransformMakeScale(0, 0)

        //添加动画
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
                self.startBtn.transform = CGAffineTransformMakeScale(1, 1)
           
            }) { (_) -> Void in
                
                print("ok")
        }
    }
   
    
    //布局子控件
    private func layoutUI(){
        
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
        //利用SnapKit框架布局子控件
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)}
        
        startBtn.snp_makeConstraints{ (make) -> Void in
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)}
        
        //想要按钮显示在中间位置,  需要相对于contentView
        startBtn.snp_makeConstraints{ (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)}
        
         //给按钮添加点击事件
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: .TouchUpInside)
    }
 
    //点击按钮的操作  addTarget 是OC的方法, 这里需要用到 @objc 进行桥接
    @objc private func startBtnDidClick(){
        
        print(__FUNCTION__)
        
        //进入欢迎界面
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    }

    
    //懒加载图片
    private lazy var iconView:UIImageView = UIImageView(image:UIImage(named: "new_feature_1"))
    
    //懒加载开始按钮
    private lazy var startBtn:UIButton = {
        
        let startBtn = UIButton()
        
        startBtn.setTitle("立即体验", forState: .Normal)
        
        startBtn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        
        startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        
        
        return startBtn
        }()
}
